import { Injectable, BadRequestException, NotFoundException } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { Role } from './entities/role.entity'

@Injectable()
export class RoleService {
  constructor(@InjectRepository(Role) private readonly role: Repository<Role>) {}

  async findAll() {
    const data = await this.role.find({
      order: {
        _id: 'ASC'
      }
    })
    if (data) {
      return { data, message: '获取角色信息成功' }
    } else {
      throw new BadRequestException('角色信息未找到！')
    }
  }

  async findOne(rid: number) {
    const data = await this.role.findOne({
      where: { role_id: rid },
      select: ['_id', 'role_id', 'role_name', 'role_desc']
    })
    if (data) {
      return { data, message: '获取角色信息成功' }
    } else {
      throw new NotFoundException('角色信息未找到！')
    }
  }
}
